// pages/userinfo/userinfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasData: false,
    name: '',
    sex: '',
    gender: '',
    phone: '',
    address: ''
  },

  onLoad: function (options) {
    var openid = wx.getStorageSync('openid')
    var that = this;
    wx.request({
      url: 'http://127.0.0.1:7001/api/user/userinfo',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        openid: openid
      },
      method: 'GET',
      success: function (result) {
        console.log(result.data);
        if (result.data.success == true&& result.data.data.result != null) {
            const {
              username,
              sex,
              phone,
              address
            } = result.data.data.result;
            that.setData({
              name: username,
              sex: sex,
              phone: phone,
              address: address,
              hasData: true
            })
          if(sex==0){
            that.setData({
              gender:'男'
            })
          } else if(sex==1){
            that.setData({
              gender:'女'
            })
          }else{
            that.setData({
              gender:'私密'
            })
          }
        }
      }
    })
  },

  onEditTap: function () {
    // 跳转到编辑页面
    wx.navigateTo({
      url: `/pages/userinfo/edit/edit?name=${this.data.name}&gender=${this.data.sex}&phone=${this.data.phone}&address=${this.data.address}`,
    });
  },

  onAddTap: function () {
    // 跳转到添加页面
    wx.navigateTo({
      url: '/pages/userinfo/add/add',
    });
  }
})